<?php
namespace Extension\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $nav = $this->get_nav();
        $goods_list = D('goods')->get_index_goods();

        $user_id = is_login();
        if($user_id) {
            $drawer_list = D('drawer')->get_user_drawer_list($user_id, 'id, title');
            $this->assign('drawer_list', $drawer_list);
        }


        $this->assign('goods_list', $goods_list);
        $this->assign('nav_list', $nav);
        $this->display('index');
    }

    //上传文件如果有传递$url参数的话就删除对应的图片
    protected function upload($file_info){
        $upload = new \Think\Upload(); //实例化tp的上传类
        $upload->exts = array('jpg','gif','png','jpeg'); //设置附件上传类型
        $upload->rootPath = './Public/Uploads/'; //相对于站点根目录jd
        $upload->savePath = ''; //设置附件上传目录,地址是相对于根目录(rootPath的)

        $info = $upload->uploadOne($_FILES['cover']); //开始上传
        if(!$info){
            $this->error($upload->getError());
        }else{
            foreach($info as $v) {
                $pic['file'] = $v['savepath'].$v['savename']; //获取文件名
                  //ori是根据需要进行更改的
                  $pic['cover'] = $v['savepath'].'400_'.$v['savename'];
                  $pic['thumb'] = $v['savepath'].'50_'.$v['savename']; //获取缩略图的文件名
                $pic['img'] = $upload->rootPath.$v['savepath'].$v['savename']; //获取完整的图片地址

                $image = new \Think\Image(); // 利用tp的图片处理类对上传的图片进行处理
                $image->open($pic['img']);
                
                  $image->thumb(400, 400)->save($upload->rootPath.$v['savepath'].'400_'.$v[savename]);
                  $image->thumb(50, 50)->save($upload->rootPath.$v['savepath'].'50_'.$v[savename]);

                return $pic; //返回相关信息数组
        }
      }
    }

    //$data
    public function upload_data($is_attachment = false)
    {
        $goods_cover = "http://wxscreen.alltosun.net/images/mall/logo.png";
        $data = file_get_contents($goods_cover);
        
        $file_dir  = './Public/Uploads'.date('/Y-m-d/');

        $file_name = time().mt_rand(1, 10000);
        $ext_name  = '.png';
        $file_path  = $file_dir.$file_name. $ext_name;

        if (!file_exists($file_dir)) {
            // 防止命令行调用生成root用户目录
            mkdir($file_dir, 0755, true);
        }

        if (file_put_contents($file_path, $data) !== false) {
            echo $file_path;
        } else {
            echo 'fail';
        }

    }

    public function discover() {
    	echo "登录成功";
    }
}